/** 
* <p>Title: DataRuleHandler.java</p> 
* <p>Description: </p> 
* <p>Copyright: Copyright (c) 2012</p> 
* <p>Company: hnii</p> 
* @author LiuC
* @date 2012-9-25 
* @version 1.0 
*/ 
package com.powerManager.sdk.handlers;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.powerManager.sdk.common.JdbcSingleton;

/** 
 * <p>Title: DataRuleHandler</p> 
 * <p>Description: 数据规则管理</p> 
 * <p>Company: hnii</p> 
 * @author    LiuC
 * @date      2012-9-25 
 */
public class DataRuleHandler {
	
	/** 
	* <p>Title: addDataRuleInfo</p> 
	* <p>Description:增加权限规则管理</br> 
	* 在新增关系规则之前，删除之前的关系规则数据
	* </p> 
	* @param request
	* @param response
	* @return 
	*/ 
	@SuppressWarnings("static-access")
	public String addDataRuleInfo(HttpServletRequest request, HttpServletResponse response){
		JdbcSingleton jdbc = new JdbcSingleton();
		// 获取数据权限id
		String id = request.getParameter("id");
		// 获取数据权限规则Id
		String ruleId = request.getParameter("ruleId");
		String[] rule = ruleId.split(",");
		int result = 0;
		// 插入数据规则管理关系表的sql语句
		String sql = "";
		if (deleteDateRuleInfo(id) && null != rule) {
			StringBuffer bf = new StringBuffer(
					"insert into sys_rule_define(rule_id,obj_id) values");
			for (int i = 0; i < rule.length; i++) {
				bf.append("('").append(id).append("',");
				bf.append("'").append(rule[i]).append("'),");
			}
			
			if (bf.toString().endsWith(",")) {
				sql = bf.toString().substring(0, bf.length() - 1);
			}
			
			result = jdbc.getJdbcTemplate().update(sql);
		}
		result = jdbc.getJdbcTemplate().update(sql);
		return result > 0 ? "text:true" : "text:false";
	}
	

	/** 
	* <p>Title: deleteDateRuleInfo</p> 
	* <p>Description: 删除权限规则管理数据</p> 
	* @param id
	* @return 
	*/ 
	@SuppressWarnings("static-access")
	public boolean deleteDateRuleInfo(String id) {
		JdbcSingleton jdbc = new JdbcSingleton();
		String sql = "delete from sys_rule_define where rule_id = ?";
		Object[] params = new Object[] { id };
		int i = 0;
		if (null != id && id.length() > 0) {
			i = jdbc.getJdbcTemplate().update(sql, params);
		}
		return i >= 0 ? true : false;
	}
}
